package com.clm.controller;

import com.clm.common.Constants;
import com.clm.interceptor.LoginInterceptor;
import com.clm.po.Food;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class FoodController extends Controller {

    @Before(LoginInterceptor.class)
    public void add(){
        String foodId = UUID.randomUUID().toString();
        getFiles(Constants.FOOD_IMG_DIR + foodId);

        String name = getPara("name");
        String price = getPara("price");
        Integer type = getParaToInt("type");
        Integer restId = getParaToInt("restId");
        String notes = getPara("notes");

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name",name);
        map.put("price",Double.parseDouble(price));
        map.put("type",type);
        map.put("rest_id",restId);
        map.put("notes",notes);
        map.put("id",foodId);

        Food food = new Food();
        food.setAttrs(map);
        food.save();
//        String id = getPara("id");
//        if(id == null || "".equals(id.trim())){
//            ResultJson json = new ResultJson();
//            json.setCode(1);
//            json.setMessage("id不能为空!");
//            Gson gson = new Gson();
//            renderJson(gson.toJson(json));
//        }
//
//        User user = User.dao.findById(id);
//
//        ResultJson json = new ResultJson();
//        if(user != null){
//            json.setCode(0);
//            json.setMessage("success");
//
//            Map<String,Object> resultMap = new HashMap<String, Object>();
//            for (Map.Entry<String, Object> entry : user.getAttrsEntrySet()) {
//                resultMap.put(entry.getKey(),entry.getValue());
//            }
//            json.setData(resultMap);
//        }else{
//            json.setCode(-1);
//            json.setMessage("faile");
//        }
//        Gson gson = new Gson();
//        renderJson(gson.toJson(json));
    }
}


